package com.jilefojing.service.impl;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.jilefojing.beans.enums.ItemTypeEnumForSource;
import com.jilefojing.beans.pojos.EBook;
import com.jilefojing.beans.pojos.base.BasePo;
import com.jilefojing.beans.pojos.enhanced.EnhancedEBook;
import com.jilefojing.beans.pojos.enhanced.EnhancedStatistics;
import com.jilefojing.beans.pojos.enhanced.base.BaseEnhanced;
import com.jilefojing.context.beans.vo.UserContext;
import com.jilefojing.dao.IEBookDao;
import com.jilefojing.service.IEBookService;
import com.jilefojing.service.IStatisticsService;
import com.jilefojing.service.base.impl.BaseService;
import com.jilefojing.util.Detect;

/**
 * 
 * @author 作者 jgshun
 * @date 创建时间 2016年09月30日 20时11分24秒
 */
@Service
public class EBookService extends BaseService<EBook> implements IEBookService {
	@Autowired
	private IEBookDao eBookDao;
	@Autowired
	private IStatisticsService statisticsService;

	@Override
	public List<EnhancedEBook> getEnhanceds(List<Long> ids, UserContext userContext) {
		List<EBook> eBooks = eBookDao.getListByIds(ids);
		if (!Detect.notEmpty(eBooks)) {
			return null;
		}
		List<EnhancedEBook> enhancedEBooks = this.convent2Enhanceds(eBooks);
		return enhancedEBooks;
	}

	@Override
	public List<EnhancedEBook> convent2Enhanceds(List<? extends BasePo> pos) {
		@SuppressWarnings("unchecked")
		List<EBook> eBooks = (List<EBook>) pos;
		List<EnhancedEBook> enhancedEBooks = new ArrayList<EnhancedEBook>();
		for (EBook _EBook : eBooks) {
			enhancedEBooks.add(new EnhancedEBook(_EBook));
		}

		return enhancedEBooks;
	}

	@Override
	public List<EnhancedEBook> appendEnhancedCommons(List<? extends BaseEnhanced> enhancedItems, UserContext userContext) {
		@SuppressWarnings("unchecked")
		List<EnhancedEBook> enhancedEBooks = (List<EnhancedEBook>) enhancedItems;
		this.appendEnhancedChildren(enhancedEBooks, userContext);
		this.appendEnhancedStatistics(enhancedEBooks, userContext);
		return enhancedEBooks;

	}

	@Override
	public EnhancedEBook appendEnhancedStatistics(EnhancedEBook enhancedEBook, UserContext userContext) {
		this.appendEnhancedStatistics(Arrays.asList(new EnhancedEBook[] { enhancedEBook }), userContext);
		return enhancedEBook;
	}

	@Override
	public List<EnhancedEBook> appendEnhancedStatistics(List<EnhancedEBook> enhancedEBooks, UserContext userContext) {

		List<Long> bookIds = new ArrayList<Long>();
		for (EnhancedEBook _EnhancedEBook : enhancedEBooks) {
			bookIds.add(_EnhancedEBook.getId());
		}
		List<EnhancedStatistics> enhancedStatisticses = statisticsService.getEnhancedStatisticses(ItemTypeEnumForSource.BOOK, bookIds, userContext);

		if (Detect.notEmpty(enhancedStatisticses)) {
			for (EnhancedEBook _EnhancedEBook : enhancedEBooks) {
				for (EnhancedStatistics _EnhancedStatistics : enhancedStatisticses) {
					if (_EnhancedStatistics.getEnhancedItem() != null) {
						if (_EnhancedEBook.getId() == ((EnhancedEBook) _EnhancedStatistics.getEnhancedItem()).getId()) {
							_EnhancedEBook.setEnhancedStatistics(_EnhancedStatistics);
							break;
						}
					}
				}
			}
		}
		return enhancedEBooks;
	}

	@Override
	public List<EnhancedEBook> appendEnhancedChildren(List<EnhancedEBook> enhancedEBooks, UserContext userContext) {
		List<Long> ids = new ArrayList<Long>();
		for (EnhancedEBook _EnhancedEBook : enhancedEBooks) {
			ids.add(_EnhancedEBook.getId());
		}
		List<EnhancedEBook> enhancedChildrent = this.getEnhancedChildrenByParentIds(ids);
		if (!Detect.notEmpty(enhancedChildrent)) {
			return enhancedEBooks;
		}

		for (EnhancedEBook _EnhancedEBook : enhancedEBooks) {
			_EnhancedEBook.setEnhancedChildren(this.getCurEnhancedChildren(_EnhancedEBook, enhancedChildrent));

		}
		return enhancedEBooks;
	}

	private List<EnhancedEBook> getCurEnhancedChildren(EnhancedEBook enhancedEBook, List<EnhancedEBook> enhancedChildrent) {
		List<EnhancedEBook> curEnhancedChildren = new ArrayList<EnhancedEBook>();
		for (EnhancedEBook _EnhancedChild : enhancedChildrent) {
			if (_EnhancedChild.getEnhancedParent().getId() == enhancedEBook.getId()) {
				curEnhancedChildren.add(_EnhancedChild);
			}
		}
		return curEnhancedChildren;
	}

	@Override
	public List<EnhancedEBook> getEnhancedChildrenByParentIds(List<Long> parentIds) {
		List<EBook> children = eBookDao.getListByParentIds(parentIds);
		if (!Detect.notEmpty(children)) {
			return null;
		}
		return this.convent2Enhanceds(children);
	}

}
